package szwolaiyun.paireach.com.szwolaizhuang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;

import szwolaiyun.paireach.com.szwolaizhuang.R;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.Operator;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.BaseEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.UserRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private Button btn_login;
    private EditText et_login_phone, et_login_password;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initControl();
    }

    private void initControl() {
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        et_login_phone = (EditText) findViewById(R.id.et_login_phone);
        et_login_password = (EditText) findViewById(R.id.et_login_password);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String name = et_login_phone.getText().toString();
                String psw = et_login_password.getText().toString();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(psw)) {
                    showToast("用户名或密码不能为空");
                } else {
                    showProgressDialog();
                    new UserRequest().passwordLogin(name, psw);
                }
                break;

        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent errorEvent) {
        super.httpErrorEvent(errorEvent);
        if (errorEvent.getRequestTag() == RequestTag.USER_LOGIN) {
            dismissProgressDialog();
            showToast(errorEvent.getErrorMessage());
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.USER_LOGIN) {
            dismissProgressDialog();
            JsonPaser.parseOperatorInfo(event.getJson());
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("operator_login_paser")) {
            if (event.getCode() == 0) {
                Operator operator = (Operator) event.getObj1();
                SharedPreferencesUtil.setValue(this, SharedPreferencesUtil.USER_NAME,operator.getUserName());
                SharedPreferencesUtil.setValue(this, SharedPreferencesUtil.REAL_NAME,operator.getRealName());
                SharedPreferencesUtil.setValue(this, SharedPreferencesUtil.ID,operator.getId()+"");
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                showToast(event.getExtraInfo());
            }
        }
    }
}
